﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace HobSoft.SoccerBet2014.Domain.Implementation.Local
{
    using Core;
    using IRepositories;
    using Models;
    using Models.KeyHelper;
    using SoccerBet2014.Common.Queryable;
    public class OddsRepository : ReadWriteBinaryRepository<Odds, string>, IOddsRepository
    {
        protected override Func<Odds, string> FindKey
        {
            get { return o => o.Key; }
        }
        public int Disable(byte bookerId, string host, params string[] oddsIds)
        {
            if (oddsIds == null)
                throw new ArgumentNullException("oddsIds", "need oddsIds");
            if (!oddsIds.Any()) return 0;

            var marketModels = BinaryContext.Current.Get<Market, string>();
            foreach (var id in oddsIds)
            {
                Odds od;
                var oddsKey = KeyGenerateHelper.Create<Odds>(bookerId, id);
                if (BinModels.TryRemove(oddsKey, out od))
                {
                    var mkey = KeyGenerateHelper.Create<Market>(od.BmID, od.GameID, host);
                    Market mk;
                    if (marketModels.TryGetValue(mkey, out mk))
                    {
                        if (mk.OddsIDS.Contains(id))
                            mk.OddsIDS.Remove(id);
                    }
                }
            }
            return oddsIds.Length;
        }

        public IList<Odds> GetNewByMarket(params string[] marketKeys)
        {
            if (marketKeys == null)
                throw new ArgumentNullException("marketKeys", "need marketKeys");
            if (!marketKeys.Any()) return null;

            var marketModels = BinaryContext.Current.Get<Market, string>();
            var query = from m in marketModels
                        join k in marketKeys on m.Key equals k.ToString()
                        select m.Value;
            List<Odds> oddsArray = new List<Odds>();
            foreach (var mk in query)
            {
                if (mk.OddsIDS == null || !mk.OddsIDS.Any()) continue;
                var queryOdds = from o in BinModels
                                join mid in mk.OddsIDS on o.Key equals mid
                                select o.Value;
                
                oddsArray.AddRange(queryOdds);
            }
            return oddsArray;
        }

        public IList<Odds> GetNewByMarket(Odds.MarketType type, params string[] marketKeys)
        {
            if (marketKeys == null)
                throw new ArgumentNullException("marketKeys", "need marketKeys");
            if (!marketKeys.Any()) return null;

            var marketModels = BinaryContext.Current.Get<Market, string>();
            var query = from m in marketModels
                        join k in marketKeys on m.Key equals k.ToString()
                        select m.Value;
            List<Odds> oddsArray = new List<Odds>();
            foreach (var mk in query)
            {
                if (mk.OddsIDS == null || !mk.OddsIDS.Any()) continue;
                var queryOdds = from o in BinModels
                                join mid in mk.OddsIDS on o.Key equals mid
                                where o.Value.Type == type
                                select o.Value;

                oddsArray.AddRange(queryOdds);
            }
            return oddsArray;
        }

    }
}
